﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;

namespace MvcApplication2.Controllers
{ 
    public class ItemController : Controller
    {
        private Context db = new Context();

        //
        // GET: /Item/

        public ViewResult Index()
        {
            return View(db.Items.ToList());
        }

        //
        // GET: /Item/Details/5

        public ViewResult Details(int id)
        {
            Item ıtem = db.Items.Find(id);
            return View(ıtem);
        }

        //
        // GET: /Item/Create

        public ActionResult Create()
        {
            ViewBag.Manufacturers = new SelectList(db.Manufacturers, "ManufacturerID", "ManufacturerName");
            return View();
        } 

        //
        // POST: /Item/Create

        [HttpPost]
        public ActionResult Create(Item ıtem)
        {
            if (ModelState.IsValid)
            {
                db.Items.Add(ıtem);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(ıtem);
        }
        
        //
        // GET: /Item/Edit/5
 
        public ActionResult Edit(int id)
        {
            Item ıtem = db.Items.Find(id);
            return View(ıtem);
        }

        //
        // POST: /Item/Edit/5

        [HttpPost]
        public ActionResult Edit(Item ıtem)
        {
            if (ModelState.IsValid)
            {
                db.Entry(ıtem).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(ıtem);
        }

        //
        // GET: /Item/Delete/5
 
        public ActionResult Delete(int id)
        {
            Item ıtem = db.Items.Find(id);
            return View(ıtem);
        }

        //
        // POST: /Item/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Item ıtem = db.Items.Find(id);
            db.Items.Remove(ıtem);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}